<?php
/*
Plugin Name: Carbon Paper :: Quick Access
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: Quick Access to Archive and Category.
Version: 1.0
Author: Bertrand SCARPA
Author URI: http://www.carbonpaper.eu
*/

register_activation_hook( __FILE__, array('Quick_access', 'activate'));
register_deactivation_hook( __FILE__, array('Quick_access', 'deactivate'));




/**
 * Display Archives for Quick Access widget.
 *
 * @param array $args Widget arguments.
 */

require_once( ABSPATH . WPINC . '/class.wp-multiwidget.php');

class Quick_access extends MultiWidget
{
  function Quick_access()
  {
    $this->MultiWidget(
        'quickaccess', // id_base
        'Carbon Paper :: Quick Access', // name
        array('description'=>__('Quick Access to Archive and Category'))
      );
  }

  function wp_dropdown_archives($args) {
	  extract($args);
	  $c = $show_count;
	  ?> 
		<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
	  <?php
  }


  // Echo the actual widget content. 
  function widget($args, $instance){
    $c = 0;
	$h = 0;
	?>
	<div class="sidebar-module">
		<h3>Accès Rapide</h3>
		<p class="first"><a href="<?php echo bloginfo('url')?>/archives"><?php echo wp_count_posts()->publish?></a> posts depuis Avril 2009.</p>

		<div class="full-archives">
			<label><?php echo $instance['archives_title']; ?></label>
			<div class="pulldown">
			<?php
			$arc_args = array('orderby' => 'name', 'show_count' => $c);
    			$arc_args['show_option_none'] = __('Select');
    			$this->wp_dropdown_archives($arc_args);
			?>
    			</div><!-- /.pulldown -->
		</div><!-- /.full-archives -->

		<div class="full-archives">
			<label><?php echo $instance['categories_title']; ?></label>
			<div class="pulldown">
			<?php
			$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'show_option_all' => 0);
    			$cat_args['show_option_none'] = __('Select');
    			wp_dropdown_categories($cat_args);?> 
			<script type="text/javascript"><!--
    			var dropdown = document.getElementById("cat");
    			function onCatChange() {
				if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
					location.href = "<?php echo get_option('home');?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
				}
    			}
    			dropdown.onchange = onCatChange;
			--></script>
    			</div><!-- /.pulldown -->
		</div><!-- /.full-archives -->

	</div><!-- /.sidebar-module -->
	<?php
  }


  // Update a particular instance.
  // This function should check that $new_instance is set correctly.
  // The newly calculated value of $instance should be returned.
  function control_update($new_instance, $old_instance)
  {
    if( !isset($new_instance['quickaccess_title']) ) // user clicked cancel
        return false;
    $instance = $old_instance;
    $instance['quickaccess_title'] = wp_specialchars( $new_instance['quickaccess_title'] );
    $instance['archives_title'] = wp_specialchars( $new_instance['archives_title'] );
    $instance['categories_title'] = wp_specialchars( $new_instance['categories_title'] );
    return $instance;
  }
	

  // Echo a control form for the current instance.
  // The form has inputs with names like widget-ID_BASE[$number][FIELD_NAME]
  // so that all data for that instance of the widget are stored in one
  // $_POST variable: $_POST['widget-ID_BASE'][$number]
  function control_form($instance)
  {
?>
    <p>

     <label for="<?php echo $this->get_field_id('quickaccess_title') ?>">
	Instance Quick Access :
      <input class="widefat" id="<?php echo $this->get_field_id('quickaccess_title') ?>"
       name="<?php echo $this->get_field_name('quickaccess_title') ?>" type="text"
       value="<?php echo htmlspecialchars($instance['quickaccess_title'],ENT_QUOTES) ?>" />
     </label>


     <label for="<?php echo $this->get_field_id('archives_title') ?>">
	Titre Archives :
      <input class="widefat" id="<?php echo $this->get_field_id('archives_title') ?>"
       name="<?php echo $this->get_field_name('archives_title') ?>" type="text"
       value="<?php echo htmlspecialchars($instance['archives_title'],ENT_QUOTES) ?>" />
     </label>

     <label for="<?php echo $this->get_field_id('categories_title') ?>">
	Titre Categories : 
      <input class="widefat" id="<?php echo $this->get_field_id('categories_title') ?>"
       name="<?php echo $this->get_field_name('categories_title') ?>" type="text"
       value="<?php echo htmlspecialchars($instance['categories_title'],ENT_QUOTES) ?>" />
     </label>

     <input type="hidden" id="<?php echo $this->get_field_id('submit') ?>"
      name="<?php echo $this->get_field_name('submit') ?>" value="1" />

    </p>
<?php
  }

} // end class ExampleMultiWidget


// Finally create an object for the widget-type and register it.
$quickaccess = new Quick_access();
add_action( 'widgets_init', array($quickaccess,'register') );

?>